package cn.iocoder.yudao.module.pay.service.point;

import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import cn.iocoder.yudao.module.pay.controller.app.point.vo.AppPayPointRechargePackagePageReqVO;
import cn.iocoder.yudao.module.pay.controller.app.point.vo.AppPayPointRechargePackageSaveReqVO;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import cn.iocoder.yudao.module.pay.controller.admin.point.vo.*;
import cn.iocoder.yudao.module.pay.dal.dataobject.point.PayPointRechargePackageDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.pay.dal.mysql.point.PayPointRechargePackageMapper;

import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.pay.enums.ErrorCodeConstants.*;

/**
 * 充值套餐 Service 实现类
 *
 * @author Jerry
 */
@Service
@Validated
public class PayPointRechargePackageServiceImpl implements PayPointRechargePackageService {

    @Resource
    private PayPointRechargePackageMapper pointRechargePackageMapper;

    @Override
    public Long createPointRechargePackage(PayPointRechargePackageSaveReqVO createReqVO) {
        // 插入
        PayPointRechargePackageDO pointRechargePackage = BeanUtils.toBean(createReqVO, PayPointRechargePackageDO.class);
        pointRechargePackageMapper.insert(pointRechargePackage);
        // 返回
        return pointRechargePackage.getId();
    }

    @Override
    public void updatePointRechargePackage(PayPointRechargePackageSaveReqVO updateReqVO) {
        // 校验存在
        validatePointRechargePackageExists(updateReqVO.getId());
        // 更新
        PayPointRechargePackageDO updateObj = BeanUtils.toBean(updateReqVO, PayPointRechargePackageDO.class);
        pointRechargePackageMapper.updateById(updateObj);
    }

    @Override
    public void deletePointRechargePackage(Long id) {
        // 校验存在
        validatePointRechargePackageExists(id);
        // 删除
        pointRechargePackageMapper.deleteById(id);
    }

    private void validatePointRechargePackageExists(Long id) {
        if (pointRechargePackageMapper.selectById(id) == null) {
            throw exception(POINT_RECHARGE_PACKAGE_NOT_EXISTS);
        }
    }

    @Override
    public PayPointRechargePackageDO getPointRechargePackage(Long id) {
        return pointRechargePackageMapper.selectById(id);
    }

    @Override
    public PageResult<PayPointRechargePackageDO> getPointRechargePackagePage(PayPointRechargePackagePageReqVO pageReqVO) {
        return pointRechargePackageMapper.selectPage(pageReqVO);
    }

    @Override
    public Long createPointRechargePackage(AppPayPointRechargePackageSaveReqVO createReqVO) {
        // 插入
        PayPointRechargePackageDO pointRechargePackage = BeanUtils.toBean(createReqVO, PayPointRechargePackageDO.class);
        pointRechargePackageMapper.insert(pointRechargePackage);
        // 返回
        return pointRechargePackage.getId();
    }

    @Override
    public void updatePointRechargePackage(AppPayPointRechargePackageSaveReqVO updateReqVO) {
        // 校验存在
        validatePointRechargePackageExists(updateReqVO.getId());
        // 更新
        PayPointRechargePackageDO updateObj = BeanUtils.toBean(updateReqVO, PayPointRechargePackageDO.class);
        pointRechargePackageMapper.updateById(updateObj);
    }

    @Override
    public PageResult<PayPointRechargePackageDO> getPointRechargePackagePage(AppPayPointRechargePackagePageReqVO pageReqVO) {
        return pointRechargePackageMapper.selectPage(pageReqVO);
    }

    @Override
    public List<PayPointRechargePackageDO> getPointRechargePackageList(Integer status) {
        return pointRechargePackageMapper.selectListByStatus(status);
    }

    @Override
    public PayPointRechargePackageDO validPointRechargePackage(Long packageId) {
        PayPointRechargePackageDO rechargePackageDO = pointRechargePackageMapper.selectById(packageId);
        if (rechargePackageDO == null) {
            throw exception(WALLET_RECHARGE_PACKAGE_NOT_FOUND);
        }
        if (CommonStatusEnum.DISABLE.getStatus().equals(rechargePackageDO.getStatus())) {
            throw exception(WALLET_RECHARGE_PACKAGE_IS_DISABLE);
        }
        return rechargePackageDO;
    }

}